package com.ruitu.health_1.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.interfaces.ILoadMoreFooter;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.ArticleModel;
import com.ruitu.health_1.mvp.presenter.ArticlePresenter;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.article.Article;

import java.util.ArrayList;
import java.util.List;

public class ArticleListActivity extends BaseRefreshActivity<ArticlePresenter.ArticleP, ArticleModel.ArticleM> {

    private DataAdapter adapter;
    private List<Integer> skipId = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_arlictle_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("文章列表");
        skipId.add(R.id.header);
        showLoadingView(skipId);
        reqFirst();

    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    private LRecyclerView l_recyclerview;

    @Override
    protected LRecyclerView findRecyclerView() {
        l_recyclerview = findViewById(R.id.l_recyclerview);
        return l_recyclerview;
    }

    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();



    }

    private int size = 1;

    @Override
    protected void reqFirst() {
        isRefresh = true;
        size = 1;
        getArticleListPage1(size);
    }

    boolean isRefresh = false;

    @Override
    protected void reqMore() {
        isRefresh = false;
        ++size;
        getArticleListPage1(size);
    }

    public void getArticleListPage1(int size) {
        p.getArticleList(10, size);
    }

    private PageModel2<Article> pageModel2;
    private List<Article> articleList = new ArrayList<>();

    public void setData(PageModel2<Article> articles) {
        showContent(skipId);
        l_recyclerview.refreshComplete(size);
        pageModel2 = articles;
        if (isRefresh) {
            articleList.clear();
        }
        articleList.addAll(articles.getList());
        adapter.setDataList(articleList);
        if (isDataEmpty(articles.getList())){
            l_recyclerview.setLoadMoreFooter(new ILoadMoreFooter() {
                @Override
                public void onReset() {

                }

                @Override
                public void onLoading() {

                }

                @Override
                public void onComplete() {

                }

                @Override
                public void onNoMore() {

                }

                @Override
                public View getFootView() {
                    TextView textView = new TextView(ArticleListActivity.this);
                    textView.setText("已经没有更多了");
                    textView.setTextColor(R.color.black);
                    textView.setGravity(Gravity.CENTER);
                    return textView;
                }
            });
        }
    }

    public void setError(){
        showError(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    class DataAdapter extends ListBaseAdapter<Article> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_article;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_article_title = holder.getView(R.id.tv_article_title);
            TextView tv_article_author = holder.getView(R.id.tv_article_author);
            TextView tv_article_content = holder.getView(R.id.tv_article_content);
            ImageView iv_img = holder.getView(R.id.iv_img);
            LinearLayout article_item = holder.getView(R.id.article_item);
            final Article article = mDataList.get(position);
            tv_article_title.setText(article.getTitle());
            tv_article_author.setText(article.getArtName());
            tv_article_content.setText(article.getReserveOne());
            Glide.with(mContext).load(article.getSmallImg()).apply(Arad.opts).into(iv_img);

            article_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Logg.i("Item点击");
                    Bundle bundle = new Bundle();
                    bundle.putString("id", article.getId());
                    startActivity(ArticleDetailsActivity.class, bundle);
                }
            });

        }

    }
}
